package com.example.woke;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.zhongcai.online.R;

public class PeaceActivity extends Activity {

	private String url, urlname;
	private WebView mWebView = null;
	private Intent intent;
	public static String TEST_IMAGE;
	private ClipData myClip;
	private ClipboardManager myClipboard;
	private TextView share;
	private String title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_html);

		intent = getIntent();
		url = intent.getStringExtra("url");
		urlname = intent.getStringExtra("title");
		intvkew();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 判断是否可以返回操作
		if (mWebView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			mWebView.goBack();

		} else {
			
			finish();
		}
		return true;

	}

	@SuppressLint("JavascriptInterface")
	private void intvkew() {

		share = (TextView) findViewById(R.id.avhtml_text_share);

		share.setVisibility(View.GONE);
		findViewById(R.id.avhtml_image_back).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mWebView.canGoBack()) {
							mWebView.goBack();
						} else {
							finish();
						}
					}
				});

		myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

		Log.e("urlname", "$" + url);
		TextView mTitel = (TextView) findViewById(R.id.avhtml_text_title);
		mTitel.setText(urlname);
		mWebView = (WebView) findViewById(R.id.webView11);
		WebSettings settings = mWebView.getSettings();
		setSettings(settings);

		// settings.setPluginsEnabled(true); // 支持插件
		// mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		// 添加js交互接口类，并起别名 imagelistner
//		mWebView.addJavascriptInterface(new JavascriptInterface(
//				getApplicationContext()), "imagelistner");
		mWebView.setWebChromeClient(new MyWebChromeClient());
		mWebView.setWebViewClient(new MyWebViewClient());
		// mWebView.loadUrl("http://www.88china.com");
		mWebView.loadUrl(url);

	}

	private void setSettings(WebSettings settings) {
		settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
		settings.setDefaultTextEncodingName("UTF-8");
		settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.setBackgroundColor(getResources().getColor(R.color.white)); // 设置背景色
		// mWebView.getBackground().setAlpha(50); // 设置填充透明度 范围：0-255
		settings.setSupportZoom(false);// 用于设置webview放大
		settings.setBuiltInZoomControls(false);
		settings.setDomStorageEnabled(true);
		settings.setTextSize(TextSize.NORMAL);
		// settings.setPluginState(PluginState.ON);
		settings.setDatabaseEnabled(true);
	}

	// js通信接口
	public class JavascriptInterface {

		private Context context;
		Intent intent = new Intent();

		public JavascriptInterface(Context context) {
			this.context = context;
		}

		public void thisImage(String index) {
			intent.putExtra("index", index);
		}
	}

	// 监听
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url != null) {
				 Uri uri = Uri.parse(url);
				 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				 startActivity(intent);
//				view.loadUrl(url);
				// finish();
				return true;
			}
			// return false;

			return super.shouldOverrideUrlLoading(view, url);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
		
			mWebView.setVisibility(View.VISIBLE);
			super.onPageFinished(view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(null, 1, "访问失败", null);
		}
	}

	private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
		}

		@Override
		public void onReceivedTouchIconUrl(WebView view, String url,
				boolean precomposed) {
			super.onReceivedTouchIconUrl(view, url, precomposed);
		}
	}

}
